﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.DanhMuc
{
    public partial class frmDonViTinh : Form
    {
        public frmDonViTinh()
        {
            InitializeComponent();
        }

        KetNoiDB cn=new KetNoiDB();
        string tag="";

        private void load_button(string tag)
        {
            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaDonViTinh.Enabled = false;
                txtTenDonViTinh.Text = "";
                txtTenDonViTinh.Enabled = true;
                txtGhiChu.Text = "";
                txtGhiChu.Enabled = true;


            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaDonViTinh.Enabled = false;
                txtTenDonViTinh.Enabled = true;
                txtGhiChu.Enabled = true;

            }
            else
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                btnLuu.Enabled = false;
                btnHuy.Enabled = false;

                txtMaDonViTinh.Enabled = false;
                txtTenDonViTinh.Enabled = false;
                txtGhiChu.Enabled = false;
            }
        }

        private void load_grid()
        {
            if(cn.KetNoi())
            {
                DataTable tbl=cn.getTable("select row_number() over(order by MaDonViTinh) as STT, * from tbl_DonViTinh");
                dtgDonViTinhGoc.DataSource=tbl;
            }
        }

        private void frmDonViTinhGoc_Load(object sender, EventArgs e)
        {
            load_button(tag);
            cn.KetNoi();

            load_grid();
        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            load_button(tag);
            txtMaDonViTinh.Text = cn.SinhMa("MaDonViTinh","tbl_DonViTinh","DV");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_button(tag);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            load_button("");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (this.TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgDonViTinhGoc.RowCount > 0)
            {
                DialogResult msg = MessageBox.Show("Bạn có chắc chắn muốn xóa?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (msg == DialogResult.Yes)
                {
                    cn.Delete_Table("tbl_DonViTinh","MaDonViTinh='"+txtMaDonViTinh.Text+"'");
                    load_button("");
                    load_grid();
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (tag == "them")
            {
                if (txtTenDonViTinh.Text == "")
                {
                    MessageBox.Show("Bạn chưa nhập Tên đơn vị tính.");
                    txtTenDonViTinh.Focus();
                    return;
                }
                string TenDonViTinh = cn.LayDuLieu("select TenDonViTinh from tbl_DonViTinh where lower(TenDonViTinh)=N'"+txtTenDonViTinh.Text.ToLower()+"'");
                if (TenDonViTinh != "")
                {
                    MessageBox.Show("Tên đơn vị tính đã tồn tại","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    txtTenDonViTinh.Focus();
                    return;
                }
                cn.Insert_Table("tbl_DonViTinh", "MaDonViTinh,TenDonViTinh,GhiChu", "'" + txtMaDonViTinh.Text + "',N'" + txtTenDonViTinh.Text + "',N'"+txtGhiChu.Text+"'");
            }
            else
            {
                cn.Update_Table("tbl_DonViTinh","TenDonViTinh=N'"+txtTenDonViTinh.Text+"',GhiChu=N'"+txtGhiChu.Text+"'","MaDonViTinh='"+txtMaDonViTinh.Text+"'");
            }
            load_button("");
            load_grid();
        }

        private void groupPanel4_Click(object sender, EventArgs e)
        {

        }

        private void dtgDonViTinhGoc_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgDonViTinhGoc.SelectedRows.Count > 0 && dtgDonViTinhGoc.CurrentRow.Cells["MaDonViTinh"].Value != null)
            {
                txtMaDonViTinh.Text = dtgDonViTinhGoc.CurrentRow.Cells["MaDonViTinh"].Value.ToString();
                txtTenDonViTinh.Text = dtgDonViTinhGoc.CurrentRow.Cells["TenDonViTinh"].Value.ToString();
                txtGhiChu.Text=dtgDonViTinhGoc.CurrentRow.Cells["colGhiChu"].Value.ToString();
            }
        }
    }
}
